home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / dns / set.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  8.7 KB  |  301 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''A simple Set class.'''
  5.  
  6. class Set(object):
  7.     '''A simple set class.
  8.  
  9.     Sets are not in Python until 2.3, and rdata are not immutable so
  10.     we cannot use sets.Set anyway.  This class implements subset of
  11.     the 2.3 Set interface using a list as the container.
  12.  
  13.     @ivar items: A list of the items which are in the set
  14.     @type items: list'''
  15.     __slots__ = [
  16.         'items']
  17.     
  18.     def __init__(self, items = None):
  19.         '''Initialize the set.
  20.  
  21.         @param items: the initial set of items
  22.         @type items: any iterable or None
  23.         '''
  24.         self.items = []
  25.         if items is not None:
  26.             for item in items:
  27.                 self.add(item)
  28.             
  29.         
  30.  
  31.     
  32.     def __repr__(self):
  33.         return 'dns.simpleset.Set(%s)' % repr(self.items)
  34.  
  35.     
  36.     def add(self, item):
  37.         '''Add an item to the set.'''
  38.         if item not in self.items:
  39.             self.items.append(item)
  40.         
  41.  
  42.     
  43.     def remove(self, item):
  44.         '''Remove an item from the set.'''
  45.         self.items.remove(item)
  46.  
  47.     
  48.     def discard(self, item):
  49.         '''Remove an item from the set if present.'''
  50.         
  51.         try:
  52.             self.items.remove(item)
  53.         except ValueError:
  54.             pass
  55.  
  56.  
  57.     
  58.     def _clone(self):
  59.         """Make a (shallow) copy of the set.
  60.  
  61.         There is a 'clone protocol' that subclasses of this class
  62.         should use.  To make a copy, first call your super's _clone()
  63.         method, and use the object returned as the new instance.  Then
  64.         make shallow copies of the attributes defined in the subclass.
  65.  
  66.         This protocol allows us to write the set algorithms that
  67.         return new instances (e.g. union) once, and keep using them in
  68.         subclasses.
  69.         """
  70.         cls = self.__class__
  71.         obj = cls.__new__(cls)
  72.         obj.items = list(self.items)
  73.         return obj
  74.  
  75.     
  76.     def __copy__(self):
  77.         '''Make a (shallow) copy of the set.'''
  78.         return self._clone()
  79.  
  80.     
  81.     def copy(self):
  82.         '''Make a (shallow) copy of the set.'''
  83.         return self._clone()
  84.  
  85.     
  86.     def union_update(self, other):
  87.         '''Update the set, adding any elements from other which are not
  88.         already in the set.
  89.         @param other: the collection of items with which to update the set
  90.         @type other: Set object
  91.         '''
  92.         if not isinstance(other, Set):
  93.             raise ValueError, 'other must be a Set instance'
  94.         
  95.         if self is other:
  96.             return None
  97.         
  98.         for item in other.items:
  99.             self.add(item)
  100.         
  101.  
  102.     
  103.     def intersection_update(self, other):
  104.         '''Update the set, removing any elements from other which are not
  105.         in both sets.
  106.         @param other: the collection of items with which to update the set
  107.         @type other: Set object
  108.         '''
  109.         if not isinstance(other, Set):
  110.             raise ValueError, 'other must be a Set instance'
  111.         
  112.         if self is other:
  113.             return None
  114.         
  115.         for item in list(self.items):
  116.             if item not in other.items:
  117.                 self.items.remove(item)
  118.                 continue
  119.         
  120.  
  121.     
  122.     def difference_update(self, other):
  123.         '''Update the set, removing any elements from other which are in
  124.         the set.
  125.         @param other: the collection of items with which to update the set
  126.         @type other: Set object
  127.         '''
  128.         if not isinstance(other, Set):
  129.             raise ValueError, 'other must be a Set instance'
  130.         
  131.         if self is other:
  132.             self.items = []
  133.         else:
  134.             for item in other.items:
  135.                 self.discard(item)
  136.             
  137.  
  138.     
  139.     def union(self, other):
  140.         '''Return a new set which is the union of I{self} and I{other}.
  141.  
  142.         @param other: the other set
  143.         @type other: Set object
  144.         @rtype: the same type as I{self}
  145.         '''
  146.         obj = self._clone()
  147.         obj.union_update(other)
  148.         return obj
  149.  
  150.     
  151.     def intersection(self, other):
  152.         '''Return a new set which is the intersection of I{self} and I{other}.
  153.  
  154.         @param other: the other set
  155.         @type other: Set object
  156.         @rtype: the same type as I{self}
  157.         '''
  158.         obj = self._clone()
  159.         obj.intersection_update(other)
  160.         return obj
  161.  
  162.     
  163.     def difference(self, other):
  164.         '''Return a new set which I{self} - I{other}, i.e. the items
  165.         in I{self} which are not also in I{other}.
  166.  
  167.         @param other: the other set
  168.         @type other: Set object
  169.         @rtype: the same type as I{self}
  170.         '''
  171.         obj = self._clone()
  172.         obj.difference_update(other)
  173.         return obj
  174.  
  175.     
  176.     def __or__(self, other):
  177.         return self.union(other)
  178.  
  179.     
  180.     def __and__(self, other):
  181.         return self.intersection(other)
  182.  
  183.     
  184.     def __add__(self, other):
  185.         return self.union(other)
  186.  
  187.     
  188.     def __sub__(self, other):
  189.         return self.difference(other)
  190.  
  191.     
  192.     def __ior__(self, other):
  193.         self.union_update(other)
  194.         return self
  195.  
  196.     
  197.     def __iand__(self, other):
  198.         self.intersection_update(other)
  199.         return self
  200.  
  201.     
  202.     def __iadd__(self, other):
  203.         self.union_update(other)
  204.         return self
  205.  
  206.     
  207.     def __isub__(self, other):
  208.         self.difference_update(other)
  209.         return self
  210.  
  211.     
  212.     def update(self, other):
  213.         '''Update the set, adding any elements from other which are not
  214.         already in the set.
  215.         @param other: the collection of items with which to update the set
  216.         @type other: any iterable type'''
  217.         for item in other:
  218.             self.add(item)
  219.         
  220.  
  221.     
  222.     def clear(self):
  223.         '''Make the set empty.'''
  224.         self.items = []
  225.  
  226.     
  227.     def __eq__(self, other):
  228.         for item in self.items:
  229.             if item not in other.items:
  230.                 return False
  231.                 continue
  232.         
  233.         for item in other.items:
  234.             if item not in self.items:
  235.                 return False
  236.                 continue
  237.         
  238.         return True
  239.  
  240.     
  241.     def __ne__(self, other):
  242.         return not self.__eq__(other)
  243.  
  244.     
  245.     def __len__(self):
  246.         return len(self.items)
  247.  
  248.     
  249.     def __iter__(self):
  250.         return iter(self.items)
  251.  
  252.     
  253.     def __getitem__(self, i):
  254.         return self.items[i]
  255.  
  256.     
  257.     def __delitem__(self, i):
  258.         del self.items[i]
  259.  
  260.     
  261.     def __getslice__(self, i, j):
  262.         return self.items[i:j]
  263.  
  264.     
  265.     def __delslice__(self, i, j):
  266.         del self.items[i:j]
  267.  
  268.     
  269.     def issubset(self, other):
  270.         '''Is I{self} a subset of I{other}?
  271.  
  272.         @rtype: bool
  273.         '''
  274.         if not isinstance(other, Set):
  275.             raise ValueError, 'other must be a Set instance'
  276.         
  277.         for item in self.items:
  278.             if item not in other.items:
  279.                 return False
  280.                 continue
  281.         
  282.         return True
  283.  
  284.     
  285.     def issuperset(self, other):
  286.         '''Is I{self} a superset of I{other}?
  287.  
  288.         @rtype: bool
  289.         '''
  290.         if not isinstance(other, Set):
  291.             raise ValueError, 'other must be a Set instance'
  292.         
  293.         for item in other.items:
  294.             if item not in self.items:
  295.                 return False
  296.                 continue
  297.         
  298.         return True
  299.  
  300.  
  301.